<template>
    <el-dialog ref="dialogform" :title="addEditName + title" :visible.sync="dialogVisible" width="40%"
        :before-close="handleClose">
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="120px" class="demo-ruleForm">
            <el-row :gutter="15">
                <el-col :span="12">
                    <el-form-item label="诊断项名称" prop="diagnosisItemId">
                        <el-select class="w100p" filterable clearable v-model="ruleForm.diagnosisItemId">
                            <el-option v-for="item of diagnosisItemList" :key="item.diagnosisItemId"
                                :label="item.diagnosisItemName" :value="item.diagnosisItemId" />
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="诊断项症状编号" prop="diagnosisItemSymbolNo">
                        <el-input class="w100p" v-model="ruleForm.diagnosisItemSymbolNo" clearable
                            placeholder="请输入诊断项症状编号" />
                    </el-form-item>
                </el-col>
            </el-row>
            <el-form-item label="特征描述" prop="diagnosisItemSymbolDesc">
                <el-input class="w100p" v-model="ruleForm.diagnosisItemSymbolDesc" clearable type="textarea" :rows="2"
                    placeholder="请输入特征描述" maxlength="300" show-word-limit />
            </el-form-item>
            <el-form-item label="特征图片" prop="fileIdList">
                <UpLoadImage v-if="dialogVisible && resetUpLoadImage" @getFileIdList="getFileIdList" :idList="fileIdList">
                </UpLoadImage>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="cancel">取 消</el-button>
            <SaveDraftBox ref="saveDraft" v-if="addEditFlag == 'add' && dialogVisible" :formData="ruleForm"
                @pushData="pushData">
            </SaveDraftBox>
            <el-button type="primary" @click="submitForm">确 定</el-button>
        </span>
    </el-dialog>
</template>
<script>
import diagnosticItemSymptom from '@/api/disease-prevention-and-control/disease-characteristic-management.js';
import UpLoadImage from "@/components-production-management/UpLoadImage";
export default {
    components: { UpLoadImage },
    props: {
        addEditFlag: {
            type: String,
            default: () => {
                return 'add'
            }
        },
        data: {
            type: Object,
            default: () => {
                return {}
            }
        },
        title: {
            type: String,
            default: () => {
                return '操作表单'
            }
        },
        dialogVisible: {
            type: Boolean,
            default: () => {
                return false
            }
        },
        diagnosisItemList: {
            type: Array,
            default: () => {
                return []
            }
        },
    },
    data() {
        return {
            pickerOptions: {
                disabledDate(time) {
                    return time.getTime() > Date.now();
                },
                shortcuts: [{
                    text: '今天',
                    onClick(picker) {
                        picker.$emit('pick', new Date());
                    }
                }, {
                    text: '昨天',
                    onClick(picker) {
                        const date = new Date();
                        date.setTime(date.getTime() - 3600 * 1000 * 24);
                        picker.$emit('pick', date);
                    }
                }, {
                    text: '一周前',
                    onClick(picker) {
                        const date = new Date();
                        date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
                        picker.$emit('pick', date);
                    }
                }]
            },
            addEditName: '添加',
            addEditApi: '',
            ruleForm: {},
            resetUpLoadImage: true,
            fileIdList: [],
            rules: {
                diagnosisItemSymbolNo: [
                    { required: true, message: "请输入", trigger: ["change", "blur"] }
                ],
                diagnosisItemId: [
                    { required: true, message: "请选择", trigger: ["change", "blur"] }
                ],
                diagnosisItemSymbolDesc: [
                    { required: true, message: "请输入", trigger: ["change", "blur"] }
                ],
                fileIdList: [
                    { required: true, message: "请上传图片", trigger: ["change", "blur"] }
                ],
            },
        }
    },
    computed: {

    },
    watch: {
        dialogVisible(v) {
            v && this.initData()
        }
    },

    mounted() { },
    methods: {
        initData() {
            if (this.addEditFlag == 'add') {
                this.resetForm();
                this.addEditApi = diagnosticItemSymptom.add;
                this.addEditName = '添加';
            } else if (this.addEditFlag == 'edit') {
                this.ruleForm = { ...this.data };
                this.fileIdList = this.data.fileIdList.split(",") || []
                this.$set(this.ruleForm, 'fileIdList', this.fileIdList);
                this.addEditApi = diagnosticItemSymptom.edit;
                this.addEditName = '编辑';
            }
        },
        pushData(data) {
            this.ruleForm = { ...data };
            this.resetUpLoadImage = false;
            this.$nextTick(() => {
                this.resetUpLoadImage = true;
                this.fileIdList = this.ruleForm.fileIdList || []
                console.log(this.fileIdList)
                this.$refs.ruleForm.clearValidate();
            })
        },
        getFileIdList(data) {
            this.$set(this.ruleForm, 'fileIdList', data.fileIdList || [])
        },
        //提交表单
        submitForm() {
            this.$refs.ruleForm.validate((valid) => {
                if (valid) {
                    if (this.ruleForm.fileIdList.length == 0) {
                        this.$message.warning('请上传图片！')
                        return
                    }
                    this.addEditApi({ ...this.ruleForm, fileIdList: this.ruleForm.fileIdList.join(',') }).then(res => {
                        let msg = res.success ? '操作成功' : '操作失败'
                        this.$message[res.success ? 'success' : 'warning'](res.message || msg)
                        res.success && this.cancel();
                        //提交数据时删除保存的记录
                        res.success && this.addEditFlag == 'add' && this.$refs.saveDraft.delByKey();
                    }).catch(err => {
                        this.$message.error(err)
                    })
                } else {
                    return false;
                }
            })
        },
        //重置
        resetForm() {
            this.fileIdList = [];
            this.ruleForm = {};
            this.$refs.ruleForm && this.$refs.ruleForm.resetFields()
        },
        //取消关闭
        cancel() {
            this.resetForm()
            this.$emit("closeDialog")
        },
        handleClose(done) {
            this.$confirm('确认关闭？')
                .then(_ => { this.resetForm(); this.cancel() })
                .catch(_ => { });
        }
    }
};
</script>
<style lang="scss" scoped>
.line {
    border-bottom: 1px solid #e6e6e6;
}

.icon {
    font-size: 20px;
    line-height: 29px;
    cursor: pointer;
}

::v-deep .noLabel {
    .el-form-item {
        .label {
            display: none;
        }

        .el-form-item__content {
            margin: 0;
        }
    }
}
</style>